<template>
  <div class="filter-conditions" :class="{'multi-conditions': isMulti}">
    <div class="conditions">
      <div class="rows">
        <div class="row" v-for="(item, index) in group.conditions" :key="item.key">
          <row
            class="columns"
            :condition.sync="item"
            @select-change="(columnIndex, value) => {selectChange(index, columnIndex, value)}">
          </row>
          <i class="el-icon-error" v-if="removable(index)" @click="removeCondition(index)"></i>
        </div>
      </div>
      <div class="relation" v-if="isMulti">
        <el-switch
          class="switch"
          v-model="group.relationType"
          active-text="且"
          active-color="#39c6cd"
          :active-value="2"
          inactive-text="或"
          inactive-color="#ff9c4b"
          :inactive-value="1">
        </el-switch>
      </div>
    </div>
    <el-button class="add-condition-btn" size="mini" icon="el-icon-circle-plus" @click="addCondition">新增一个条件</el-button>
  </div>
</template>

<script>
  import Row from './Row'

  export default {
    components: {
      Row
    },
    props: {
      group: {
        type: Object,
        default: () => ({
          index: 0,
          key: 1,
          relationType: 2,
          conditions: []
        })
      }
    },
    computed: {
      isMulti() {
        return this.group.conditions.length > 1
      }
    },
    data() {
      return {

      }
    },
    methods: {
      removable(index) {
        return !(this.groupIndex === 0 && index === 0)
      },
      addCondition() {
        this.$emit('add-condition', this.group.index)
      },
      selectChange(conditionIndex, columnIndex, value) {
        console.log('index: %o; value: %o', conditionIndex, value)
        this.$emit('select-change', conditionIndex, columnIndex, value)
      }
    }
  }
</script>

<style lang="scss" scoped>

  .filter-conditions {
    margin-bottom: 20px;
    &:last-child {
      margin-bottom: 0;
    }
    &.multi-conditions {
      $inputHeight: 40px;
      $lineColor: #DCDFE6;

      .conditions {
        position: relative;
        box-sizing: border-box;
        padding-left: 108px;

        .rows:after{
          content: "";
          position: absolute;
          top: $inputHeight / 2;
          left: -64px;
          display: block;
          height: calc(100% - #{$inputHeight});
          width: 1px;
          background-color: $lineColor;
        }
        .row:first-child:after,
        .row:last-child:after {
          content: "";
          position: absolute;
          top: 50%;
          left: -64px;
          display: block;
          width: 64px;
          height: 1px;
          background-color: $lineColor;
        }

      }
      .add-condition-btn {
        margin-left: 108px;
      }
    }

    .rows {
      position: relative;
    }

    .row {
      position: relative;
      margin-bottom: 10px;
      overflow: visible;
      &:last-child {
        margin-bottom: 0;
      }
      .columns{
        display: inline-block;
      }
      .el-icon-error{
        display: inline-block;
        margin-left: 10px;
        color: #999;
        cursor: pointer;
        &:hover{
          color: #ff4545;
        }
      }
    }

    .relation {
      position: absolute;
      top: 50%;
      left: 0;
      z-index: 1;
      transform: translateY(-50%);
    }

    .add-condition-btn {
      margin-top: 20px;
    }
  }

</style>
